<?php
/*
 * 找伙伴
 */
class FindPeopleApi extends Api {
	function rank_score() {
		$rank=0;
		$type  = $this->data['type'] ? $this->data['type'] : 1;//排名类型 1按月排名  2按年排名
		$total = $this->data['total'] ? $this->data['total'] : 10;//排名数量
		$user = model ( 'User' )->getUserInfo ( $this->mid );
		$my ['uname'] = $user ['uname'];
		$my ['avatar'] = $user ['avatar_big'];
		//获取教师用户
		if($type == 1) {
			$begin = strtotime( date('Y').date('m').'01' );
			$end   = strtotime(date('Y').date('m').date('t'));
			$map['publish_time'] = array( array('gt',$begin) ,  array('lt',$end)) ;
		} else {
			$begin = strtotime( date('Y',time()).'0101' );
			$end   = strtotime( date('Y',time()).'1231' );
			$map['publish_time'] = array( array('gt',$begin) ,  array('lt',$end)) ;
		}
		$uids=model("User")->where("no_login !=1")->field("uid")->select();
		$uids=getSubByKey($uids,"uid");
		$where["uid"]=array('in',$uids);
		$where["user_group_id"]=7;
		$user = M('user_group_link')->where($where)->field('uid')->limit($total)->findAll();
		foreach($user as &$val) {
			$map['uid'] = $val['uid'];
			//$map['publish_time'] = 
			$val['feed_id'] = M('feed')->where($map)->field('feed_id')->findAll();
			$val['feed_id'] = getSubByKey($val['feed_id'] , 'feed_id');
			$val['digg_count'] = intval( M('feed_digg')->where( array('feed_id'=>array('in' , $val['feed_id'])) )->count() );
			unset($val['feed_id']);
		}
		
		// 积分排行
		$scoreuids = arr_sort($user ,'digg_count' , 'desc');
		$iscore = 0;
		foreach ( $scoreuids as $key => $gu ) {
			$iscore ++;
			
			$gu ['uid'] == $this->mid && $rank = $iscore;
			$gu ['rank'] = ( string ) $iscore;
			$user = model ( 'User' )->getUserInfo ( $gu ['uid'] );
			$gu ['uname'] = $user ['uname'];
			$gu ['avatar'] = $user ['avatar_big'];
			
			$map ['key'] = 'weibo_count';
			$map ['uid'] = $gu ['uid'];
			$gu ['weibo_count'] = ( string ) M ( 'user_data' )->where ( $map )->getField ( 'value' );
			
			$lists [] = $gu;
		}

		$my ['rank'] = $rank==0? '排名：'.$total."名以后" : '排名：' . $rank;
		$my ['lists'] = $lists ? $lists : array();
		return $my;
	}
	
	function rank_medal() {
		$user = model ( 'User' )->getUserInfo ( $this->mid );
		$my ['uname'] = $user ['uname'];
		$my ['avatar'] = $user ['avatar_big'];
		
		// 勋章排行
		$medaluids = M ( 'medal_user' )->field ( 'uid,count(medal_id) as mcount' )->group ( 'uid' )->order ( 'mcount desc,uid' )->limit ( 10000 )->findAll ();
		$imedal = 0;
		foreach ( $medaluids as $key => $mu ) {
			$imedal ++;
			
			$mu ['uid'] == $this->mid && $rank = $imedal;
			if ($key < 14) {
				$mu ['rank'] = ( string ) $imedal;
				$mu ['mcount'] = ( string ) $mu ['mcount'];
				$user = model ( 'User' )->getUserInfo ( $mu ['uid'] );
				$mu ['uname'] = $user ['uname'];
				$mu ['avatar'] = $user ['avatar_big'];
				
				$lists [] = $mu;
			}
		}
		// empty ( $rank ) && $rank = 10000; // 一万名后不再作排名，以提高性能
		
		$my ['rank'] = '排名：' . $rank;
		$my ['lists'] = $lists;
		
		return $my;
	}
	/**
	 * 找人首页-搜索用户 --using
	 *
	 * @param string $key
	 *        	搜索关键词
	 * @param string $max_id
	 *        	上次返回的最后一个用户ID
	 * @param string $count
	 *        	数量
	 * @return array 用户列表
	 */
	public function search_user() {
		$map['user_group_id']=$this->data["user_type"]==1 ? 6 :7 ;
		$uids=model("UserGroupLink")->where($map)->field("uid")->select();
		$uids = getSubByKey ( $uids, 'uid' );
		unset($map);
		$key = trim ( t ( $this->data ['key'] ) );
		if ($key) {
			$map ['uid'] = array (
					'neq',
					$this->mid 
			);
			$map['uid'] =array("in",$uids);
			$map ['is_init'] = 1;
			$map ['is_audit'] = 1;
			$map ['is_active'] = 1;
			$map ['is_del'] = 0;
			$max_id = $this->max_id ? intval ( $this->max_id ) : 0;
			$count = $this->count ? intval ( $this->count ) : 20;
			$map2 = $map;
			$map2 ['uname'] = $key;
			$uid_arr = model ( 'User' )->where ( $map2 )->field ( 'uid,uname,intro' )->findAll (); // 先搜索和key一致的，优先显示
			if ($uid_arr) {
				$map ['uid'] = array (
						'neq',
						$uid_arr [0] ['uid'] 
				);
				! empty ( $key ) && $map ['search_key'] = array (
						'like',
						'%' . $key . '%' 
				);
				if (! $max_id) {
					$user_list = model ( 'User' )->where ( $map )->field ( 'uid,uname,intro' )->order ( 'uid desc' )->limit ( $count - 1 )->findAll ();
					$user_list = array_merge ( $uid_arr, $user_list );
				} else {
					$map ['uid'] = array (
							array (
									'lt',
									$max_id 
							),
							array (
									'neq',
									$uid_arr [0] ['uid'] 
							),
							'AND' 
					);
					$user_list = model ( 'User' )->where ( $map )->field ( 'uid,uname,intro' )->order ( 'uid desc' )->limit ( $count )->findAll ();
				}
			} else {
				! empty ( $max_id ) && $map ['uid'] = array (
						'lt',
						$max_id 
				);
				! empty ( $key ) && $map ['search_key'] = array (
						'like',
						'%' . $key . '%' 
				);
				$user_list = ( array ) model ( 'User' )->where ( $map )->field ( 'uid,uname,intro' )->order ( 'uid desc' )->limit ( $count )->findAll ();
			}
			$follow_status = model ( 'Follow' )->getFollowStateByFids ( $this->mid, getSubByKey ( $user_list, 'uid' ) );
			if($user_list){
				foreach ( $user_list as $k => $v ) {
					$user_list [$k] ['intro'] = $user_list [$k] ['intro'] ? $user_list [$k] ['intro'] : '';
					$user_list [$k] ['follow_status'] = $follow_status [$v ['uid']];
					$user_info = api ( 'User' )->get_user_info ( $v ['uid'] );
					$user_list [$k] ['avatar'] = $user_info ['avatar'] ['avatar_big'];
				}
			}
		} else { // 获取感兴趣的5个人
			$user = model ( 'RelatedUser' )->getRelatedUser ( 5 ,$user_type);
			$user_list = array ();
			foreach ( $user as $k => $v ) {
				$user_list [$k] ['uid'] = $v ['userInfo'] ['uid'];
				$user_list [$k] ['uname'] = $v ['userInfo'] ['uname'];
				$user_list [$k] ['avatar'] = $v ['userInfo'] ['avatar_big'];
				$user_list [$k] ['intro'] = $v ['info'] ['msg'] ? $v ['info'] ['msg'] : '';
				$user_list [$k] ['follow_status'] = model ( 'Follow' )->getFollowState ( $this->mid, $v ['userInfo'] ['uid'] );
			}
		}
		return $user_list;
	}
	
	/**
	 * 按标签搜索 --using
	 *
	 * @return array 所有标签分类
	 */
	function get_user_tags() {
		$level1 = D ( 'user_category' )->where ( 'pid=0' )->order ( 'sort asc,user_category_id asc' )->findAll ();
		$categoryTree = array ();
		foreach ( $level1 as $k => $v ) {
			$categoryTree [$k] ['title'] = $v ['title'];
			$categoryTree [$k] ['child'] = D ( 'user_category' )->where ( 'pid=' . $v ['user_category_id'] )->field ( 'user_category_id as id,title' )->findAll ();
		}
		return $categoryTree;
	}
	
	/**
	 * 按标签搜索用户 --using
	 *
	 * @param
	 *        	integer tag_id 标签ID
	 * @param
	 *        	integer max_id 上次返回的最后一个用户ID
	 * @param string $count
	 *        	数量
	 * @return array 用户列表
	 */
	public function search_by_tag() {
		$max_id = $this->max_id ? intval ( $this->max_id ) : 0;
		$count = $this->count ? intval ( $this->count ) : 20;
		$cid = intval ( $this->data ['tag_id'] );
		if (! $cid) {
			return array (
					'status' => 0,
					'msg' => '请选择标签' 
			);
		}
		$pid = M ( 'UserCategory' )->where ( 'user_category_id=' . $cid )->getField ( 'pid' );
		if ($pid == 0) {
			$cids = M ( 'UserCategory' )->where ( 'pid=' . $cid )->getAsFieldArray ( 'user_category_id' );
			
			$cmap ['user_category_id'] = array (
					'IN',
					$cids 
			);
			
			$title = M ( 'UserCategory' )->where ( $cmap )->findAll ();
			
			foreach ( $title as $key => $value ) {
				$amap ['name'] = array (
						'LIKE',
						$value ['title'] 
				);
				$tag = M ( 'tag' )->where ( $amap )->getField ( 'tag_id' );
				if ($tag) {
					$tag_id [] = $tag;
				}
			}
			$tmap ['tag_id'] = array (
					'IN',
					$tag_id 
			);
		} else {
			$cmap ['user_category_id'] = intval ( $cid );
			$title = M ( 'UserCategory' )->where ( $cmap )->find ();
			$amap ['name'] = array (
					'LIKE',
					$title ['title'] 
			);
			$tag_id [] = M ( 'tag' )->where ( $amap )->getField ( 'tag_id' );
			$tmap ['tag_id'] = array (
					'IN',
					$tag_id 
			);
		}
		! empty ( $max_id ) && $tmap ['row_id'] = array (
				'lt',
				$max_id 
		);
		$uids = M ( 'app_tag' )->field ( '`row_id`' )->where ( $tmap )->order ( 'row_id desc' )->limit ( $count )->findAll ();
		
		$user_list = array ();
		foreach ( $uids as $k => $v ) {
			$user_info = api ( 'User' )->get_user_info ( $v ['row_id'] );
			$user_list [$k] ['uid'] = $user_info ['uid'];
			$user_list [$k] ['uname'] = $user_info ['uname'];
			$user_list [$k] ['avatar'] = $user_info ['avatar'] ['avatar_big'];
			$user_list [$k] ['intro'] = $user_info ['intro'] ? $user_info ['intro'] : '';
			$user_list [$k] ['follow_status'] = model ( 'Follow' )->getFollowState ( $this->mid, $v ['row_id'] );
		}
		return $user_list;
	}
	
	/**
	 * 获取地区(按字母) --using
	 *
	 * @return array 城市列表
	 */
	public function get_user_city() {
		$my = model('User')->where('`uid` = ' . $this->mid)->getField('city');
		$letters = array (
				'my' => array (),
				'A' => array (),
				'B' => array (),
				'C' => array (),
				'D' => array (),
				'E' => array (),
				'F' => array (),
				'G' => array (),
				'H' => array (),
				'I' => array (),
				'J' => array (),
				'K' => array (),
				'L' => array (),
				'M' => array (),
				'N' => array (),
				'O' => array (),
				'P' => array (),
				'Q' => array (),
				'R' => array (),
				'S' => array (),
				'T' => array (),
				'U' => array (),
				'V' => array (),
				'W' => array (),
				'X' => array (),
				'Y' => array (),
				'Z' => array () 
		);
		$provinces = D ( 'area' )->where ( 'pid=0' )->findAll ();
		$map ['pid'] = array (
				'in',
				getSubByKey ( $provinces, 'area_id' ) 
		);
		$map ['title'] = array (
				'exp',
				'not in("市辖区","县","市","省直辖县级行政单位" ,"省直辖行政单位")' 
		);
		$citys = D ( 'area' )->where ( $map )->findAll ();
		
		foreach ( $citys as $k => $v ) {
			$first_letter = getFirstLetter ( $v ['title'] );
			$letters [$first_letter] [$v ['area_id']] ['city_id'] = $v ['area_id'];
			$letters [$first_letter] [$v ['area_id']] ['city_name'] = $v ['title'];
			if ($v['area_id'] == $my) {
				$letters ['my'] [$v ['area_id']] ['city_id'] = $v ['area_id'];
				$letters ['my'] [$v ['area_id']] ['city_name'] = $v ['title'];
			}
			unset ( $first_letter );
		}
		return $letters;
	}
	
	/**
	 * 按地区搜索用户 --using
	 *
	 * @param
	 *        	integer city_id 城市ID
	 * @param
	 *        	integer max_id 上次返回的最后一个用户ID
	 * @param string $count
	 *        	数量
	 * @return array 用户列表
	 */
	public function search_by_city() {
		$max_id = $this->max_id ? intval ( $this->max_id ) : 0;
		$count = $this->count ? intval ( $this->count ) : 20;
		$city_id = intval ( $this->data ['city_id'] );
		if (! $city_id) {
			return array (
					'status' => 0,
					'msg' => '请选择地区' 
			);
		}
		! empty ( $max_id ) && $map ['uid'] = array (
				'lt',
				$max_id 
		);
		$map ['city'] = $city_id;
		$map ['is_init'] = 1;
		$uids = model ( 'User' )->where ( $map )->order ( 'uid desc' )->field ( 'uid' )->limit ( $count )->findAll ();
		$user_list = array ();
		foreach ( $uids as $k => $v ) {
			$user_info = api ( 'User' )->get_user_info ( $v ['uid'] );
			$user_list [$k] ['uid'] = $user_info ['uid'];
			$user_list [$k] ['uname'] = $user_info ['uname'];
			$user_list [$k] ['avatar'] = $user_info ['avatar'] ['avatar_big'];
			$user_list [$k] ['intro'] = $user_info ['intro'] ? $user_info ['intro'] : '';
			$user_list [$k] ['follow_status'] = model ( 'Follow' )->getFollowState ( $this->mid, $v ['uid'] );
		}
		return $user_list;
	}
	
	/**
	 * 获取认证分类 --using
	 *
	 * @return array 所有认证分类
	 */
	function get_user_verify() {
		$categoryTree = model ( 'UserGroup' )->where ( 'is_authenticate=1' )->field ( 'user_group_id as verify_id,user_group_name as title' )->findAll ();
		foreach ( $categoryTree as $k => $v ) {
			$child = D ( 'user_verified_category' )->where ( 'pid=' . $v ['verify_id'] )->field ( 'user_verified_category_id,title' )->findAll ();
			if ($child) {
				foreach ( $child as $k1 => $v1 ) {
					$categoryTree [$k] ['child'] [$k1] ['verify_id'] = $v ['verify_id'] . '_' . $v1 ['user_verified_category_id'];
					$categoryTree [$k] ['child'] [$k1] ['title'] = $v1 ['title'];
				}
			} else {
				$categoryTree [$k] ['child'] = array ();
			}
		}
		return $categoryTree;
	}
	
	/**
	 * 按认证搜索用户 --using
	 *
	 * @param
	 *        	integer verify_id 认证类型ID
	 * @param
	 *        	integer max_id 上次返回的最后一个ID
	 * @param string $count
	 *        	数量
	 * @return array 用户列表
	 */
	public function search_by_verify() {
		$max_id = $this->max_id ? intval ( $this->max_id ) : 0;
		$count = $this->count ? intval ( $this->count ) : 20;
		$verify_id = t ( $this->data ['verify_id'] );
		if (! $verify_id) {
			return array (
					'status' => 0,
					'msg' => '请选择认证类型' 
			);
		}
		
		$verify_arr = explode ( '_', $verify_id );
		$map ['usergroup_id'] = intval ( $verify_arr [0] );
		if ($verify_arr [1]) {
			$map ['user_verified_category_id'] = intval ( $verify_arr [1] );
		}
		! empty ( $max_id ) && $map ['id'] = array (
				'lt',
				$max_id 
		);
		$map ['verified'] = 1;
		$uids = D ( 'user_verified' )->where ( $map )->field ( 'id,uid' )->order ( 'id desc' )->limit ( $count )->findAll ();
		$user_list = array ();
		foreach ( $uids as $k => $v ) {
			$user_list [$k] ['id'] = $v ['id'];
			$user_info = api ( 'User' )->get_user_info ( $v ['uid'] );
			$user_list [$k] ['uid'] = $user_info ['uid'];
			$user_list [$k] ['uname'] = $user_info ['uname'];
			$user_list [$k] ['avatar'] = $user_info ['avatar'] ['avatar_big'];
			$user_list [$k] ['intro'] = $user_info ['intro'] ? $user_info ['intro'] : '';
			$user_list [$k] ['follow_status'] = model ( 'Follow' )->getFollowState ( $this->mid, $v ['uid'] );
		}
		return $user_list;
	}

	/**
	 * 更新用户当前地理位置信息
	 *
	 * @return array
	 * @author Seven Du <lovevipdsw@vip.qq.com>
	 **/
	public function updateUserLocation()
	{
		/**
		 * 纬度
		 * @var float
		 */
		$lat = floatval($this->data['latitude']);

		/**
		 * 经度
		 * @var float
		 */
		$lng = floatval($this->data['longitude']);

		/* # 判断是否存在，存在则进行下一步否则添加信息 */
		if (!D('mobile_user')->where('`uid` = ' . $this->mid)->field('uid')->count()) {
			$userData = model('User')->where('`uid` = ' . $this->mid)->field('`uname`, `intro`, `sex`')->find();
			D('mobile_user')->add(array(
				'nickname'   => $userData['uname'],
				'infomation' => $userData['intro'],
				'sex'        => $userData['sex'],
				'uid'        => $this->mid
			));
			return array(
				'status'  => 1,
				'message' => '位置添加成功'
			);

		/* 判断是否更新成功 */
		} elseif (D('mobile_user')->where('`uid` = ' . $this->mid)->save(array(
			'last_latitude'  => $lat,
			'last_longitude' => $lng
		))) {
			return array(
				'status'  => 1,
				'message' => '位置更新成功'
			);
		}
		return array(
			'status'  => 0,
			'message' => '位置未改变'
		);
	}
	
	/**
	 * 搜索附近的人 --using
	 *
	 * @param
	 *        	float latitude 纬度
	 * @param
	 *        	float longitude 经度
	 */
	// public function search_by_distinct() {
	// 	$latitude = floatval ( $this->data ['latitude'] );
	// 	$longitude = floatval ( $this->data ['longitude'] );
	// 	$distance = 10000;
	// 	$distances = ($distance / 1000);
	// 	$range = 180 / pi () * $distances / 6372.797; // 里面的 distance 就代表搜索 1km 之内，单位km
	// 	$lngR = $range / cos ( $latitude * pi () / 180 );
	// 	$maxLat = $latitude + $range; // 最大纬度
	// 	$minLat = $latitude - $range; // 最小纬度
	// 	$maxLng = $longitude + $lngR; // 最大经度
	// 	$minLng = $longitude - $lngR; // 最小经度
	// 	$where = "last_latitude > {$minLat} and last_latitude < {$maxLat} and last_longitude > {$minLng} and last_longitude < {$maxLng}";
	// 	$where .= " and uid != {$this->mid}";
	// 	$field = "uid,last_latitude,last_longitude,SQRT(POW((last_latitude-{$longitude}),2)+POW((last_longitude-{$latitude}),2)) as distincts";
	// 	$order = "distincts ASC";
	// 	$limit = ' limit 20';
	// 	$sql = "select " . $field . " from " . C ( 'DB_PREFIX' ) . "mobile_user where " . $where . " order by " . $order . $limit . ";";
	// 	// dump($sql);
	// 	$data = D ()->query ( $sql );
	// 	// dump($data);
	// 	$user_list = array ();
	// 	foreach ( $data as $k => $v ) {
	// 		$user_list [$k] ['distinct'] = $this->getDistinct ( $latitude, $longitude, $v ['last_latitude'], $v ['last_longitude'] );
	// 		$user_info = api ( 'User' )->get_user_info ( $v ['uid'] );
	// 		$user_list [$k] ['uid'] = $user_info ['uid'];
	// 		$user_list [$k] ['uname'] = $user_info ['uname'];
	// 		$user_list [$k] ['avatar'] = $user_info ['avatar'] ['avatar_big'];
	// 		$user_list [$k] ['intro'] = $user_info ['intro'] ? $user_info ['intro'] : '';
	// 		$user_list [$k] ['	'] = model ( 'Follow' )->getFollowState ( $this->mid, $v ['uid'] );
	// 	}
	// 	return $user_list;
	// }
	
	/**
	 * 附近的人API
	 *
	 * @return array 附近的人列表
	 * @author Medz Seven <lovevipdsw@vip.qq.com>
	 **/
	public function around()
	{
		/**
		 * 纬度
		 * @var float
		 */
		$lat = floatval($this->data['latitude']);

		/**
		 * 经度
		 * @var float
		 */
		$lng = floatval($this->data['longitude']);

		/**
		 * 计算多大的范围，单位km
		 * @var integer
		 */
		$distance = 1;

		/**
		 * 地球平均半径
		 * @var float
		 */
		$earthRadius = 6371.393;

		/**
		 * 计算纬度范围半径
		 * @var float
		 */
		// $dlat = $distance / $earthRadius;
		// $dlat = rad2deg($dlat);

		/**
		 * 计算经度范围半径
		 * @var float
		 */
		// $dlng = 2 * asin(sin($distance / (2 * $earthRadius)) / cos(deg2rad($lat)));
		// $dlng = rad2deg($dlng);

		/**
		 * 数据库查询条件
		 * @var string
		 */
		// $where = '`last_latitude` != 0 AND `last_latitude` > %s AND `last_latitude` < %s AND `last_longitude` > %s AND `last_longitude` < %s';
		// $where = sprintf($where, $lat - $dlat, $lat + $dlat, $lng - $dlng, $lng + $dlng);
		// $where.= ' AND `uid` != ' . $this->mid;
		
		/**
		 * 保证分页页码位于get全局变量
		 */
		$_GET['p'] = $_REQUEST['p'];

		// $_GET['p'] = $this->data['page'];

		/**
		 * 每页数据条数
		 * @var integer
		 */
		$dataNum   = 20;

		/**
		 * 查询的数据数组
		 * @var array
		 */
		// $list = D('mobile_user')->where($where)->field('`uid`, `last_latitude`, `last_longitude`, SQRT(POW((last_latitude - ' . $lng . '), 2) + POW((last_longitude - ' . $lat . '), 2)) AS `sort`')->order('`sort` ASC')->findPage($dataNum);

		/* SELECT *, ( 6371 * acos( cos( radians(30.569166) ) * cos( radians( last_latitude ) ) * cos( radians( last_longitude ) - radians(104.059296) ) + sin( radians(30.569166) ) * sin( radians( last_latitude ) ) ) ) AS distance FROM `ts_mobile_user` HAVING distance < 1 ORDER BY distance ASC LIMIT 0 , 20 */

		$distanceSql = '( ' . $earthRadius . ' * acos( cos( radians(' . $lat . ') ) * cos( radians( last_latitude ) ) * cos( radians( last_longitude ) - radians(' . $lng . ') ) + sin( radians(' . $lat . ') ) * sin( radians( last_latitude ) ) ) )';

		$field = '`uid`, `last_latitude`, `last_longitude`, ' . $distanceSql . ' AS `distance`';

		// $count = D('mobile_user')->field('count(1) as `num`, ' . $distanceSql . ' AS `distance`')->having('`distance` < ' . $distance)->find();
		// $count = $count['num'];

		/* 
			SELECT count(*) FROM (SELECT ( 6371.393 * acos( cos( radians(30.569166) ) * cos( radians( last_latitude ) ) * cos( radians( last_longitude ) - radians(104.059296) ) + sin( radians(30.569166) ) * sin( radians( last_latitude ) ) ) ) AS distance FROM `ts_mobile_user` HAVING distance < 1) as `table`
		 */
		$count = 'SELECT count(*) AS `num` FROM (SELECT ' . $distanceSql . ' AS `distance` FROM `%s` HAVING `distance` < %d) AS `table`';
		$count = sprintf($count, D('mobile_user')->getTableName(), $distance);
		$count = D()->query($count);
		$count = $count[0]['num'];

		$list  = D('mobile_user')->having('`distance` < ' . $distance)->order('`distance` ASC')->field($field)->findPage($dataNum, $count);

		// $list = D('mobile_user')->findPageBySql();

		// dump($list, D('mobile_user'));exit;

		foreach ($list['data'] as $key => $value) {
			/**
			 * 用户数据
			 * @var array
			 */
			$userData = D('User')->getUserInfo($value['uid']);

			/**
			 * 临时数据
			 * @var array
			 */
			$data     = array();

			/**
			 * 用户UID
			 */
			$data['uid']      = $userData['uid'];

			/**
			 * 用户名
			 */
			$data['username'] = $userData['uname'];

			/**
			 * 用户距离
			 */
			// $data['distance'] = $this->getDistinct($lat, $lng, $value['last_latitude'], $value['last_longitude']);
			$data['distance'] = intval($value['distance'] * 1000);

			/**
			 * 用户头像
			 */
			$data['avatar'] = $userData['avatar_big'];

			/**
			 * 当前用户对该用户的关注状态
			 */
			$data['followStatus'] = model('Follow')->getFollowState($this->mid, $userData['uid']);


			/**
			 * 用户简介
			 */
			$data['intro'] = $userData['intro'];

			/**
			 * 将临时数据替换为正式数据
			 */
			$list['data'][$key] = $data;
		}
		unset($data, $userData, $value, $key, $list['html'], $list['totalRows']);

		/**
		 * 返回数据
		 */
		return $list;
	}

	/**
	 * 获取用户与当前位置之间的距离 单位m
	 *
	 * @param float $nowLat 当前纬度
	 * @param float $nowLng 当前经度
	 * @param float $userLat 计算的用户纬度
	 * @param float $userLng 计算的用户经度
	 * @return string 单位数值
	 * @author Medz Seven <lovevipdsw@vip.qq.com>
	 **/
	protected function getDistinct($nowLat, $nowLng, $userLat, $userLng)
	{
		$earthRadius = 6371393; //approximate radius of earth in meters

		/*
		Convert these degrees to radians
		to work with the formula
		*/

		$nowLat = ($nowLat * pi() ) / 180;
		$nowLng = ($nowLng * pi() ) / 180;

		$userLat = ($userLat * pi() ) / 180;
		$userLng = ($userLng * pi() ) / 180;

		/*
		Using the
		Haversine formula

		http://en.wikipedia.org/wiki/Haversine_formula

		calculate the distance
		*/

		$calcLongitude = $userLng - $nowLng;
		$calcLatitude  = $userLat - $nowLat;

		$stepOne = pow(sin($calcLatitude / 2), 2) + cos($nowLat) * cos($userLat) * pow(sin($calcLongitude / 2), 2);  $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
		$calculatedDistance = $earthRadius * $stepTwo;

		return round($calculatedDistance);
	}
	
	/**
	 * 根据经纬度获取两点之间的距离 --using
	 *
	 * @param float $myLat
	 *        	纬度
	 * @param float $myLng
	 *        	经度
	 * @param float $userLat
	 *        	纬度
	 * @param float $userLng
	 *        	经度
	 * @return float 距离
	 */
	// private function getDistinct($myLat, $myLng, $userLat, $userLng) {
	// 	$earthRadius = 6367000; // approximate radius of earth in meters
	// 	$lat1 = ($myLat * pi ()) / 180;
	// 	$lng1 = ($myLng * pi ()) / 180;
	// 	$lat2 = ($userLat * pi ()) / 180;
	// 	$lng2 = ($userLng * pi ()) / 180;
	// 	$calcLongitude = $lng2 - $lng1;
	// 	$calcLatitude = $lat2 - $lat1;
	// 	$stepOne = pow ( sin ( $calcLatitude / 2 ), 2 ) + cos ( $lat1 ) * cos ( $lat2 ) * pow ( sin ( $calcLongitude / 2 ), 2 );
	// 	$stepTwo = 2 * asin ( min ( 1, sqrt ( $stepOne ) ) );
	// 	$calculatedDistance = round ( $earthRadius * $stepTwo / 1000, 1 );
	// 	return $calculatedDistance . 'km';
	// }
	
	/**
	 * 根据通讯录搜索用户 --using
	 *
	 * @param
	 *        	string tel 以逗号连接的手机号码串
	 * @return array
	 */
	public function search_by_tel() {
		$tel_array = array_unique ( array_filter ( explode ( ',', $this->data ['tel'] ) ) );
		$data = array ();
		$user_list = array ();
		$user_list1 = array ();
		if ($tel_array) {
			foreach ( $tel_array as $k => $v ) {
				if (preg_match ( "/^[1][358]\d{9}$/", t ( $v ) ) !== 0) {
					if ($uid = model ( 'User' )->where ( array (
							'login' => t ( $v ) 
					) )->getField ( 'uid' )) {
						$user_info = api ( 'User' )->get_user_info ( $uid );
						$user_list [$k] ['tel'] = $v;
						$user_list [$k] ['uid'] = $user_info ['uid'];
						$user_list [$k] ['uname'] = $user_info ['uname'];
						$user_list [$k] ['avatar'] = $user_info ['avatar'] ['avatar_big'];
						$user_list [$k] ['intro'] = $user_info ['intro'] ? $user_info ['intro'] : '';
						$user_list [$k] ['follow_status'] = model ( 'Follow' )->getFollowState ( $this->mid, $user_info ['uid'] );
					} else {
						$user_list1 [$k] ['uid'] = 0;
						$user_list1 [$k] ['tel'] = $v;
					}
				}
			}
			$data = array_merge ( $user_list, $user_list1 );
		}
		return $data;
	}
	function top_ad() {
		$map ['place'] = 127;
		$map ['display_type'] = 3;
		
		$info = M ( 'ad' )->where ( $map )->order ( 'display_order desc, ctime desc' )->find ();
		// dump(M()->getLastSql());
		$info ['content'] = unserialize ( $info ['content'] );
		// 获取附件图片地址
		foreach ( $info ['content'] as &$val ) {
			$attachInfo = model ( 'Attach' )->getAttachById ( $val ['banner'] );
			$val ['bannerpic'] = getImageUrl ( $attachInfo ['save_path'] . $attachInfo ['save_name'] );
		}
		// dump ( $info );
		
		return $info;
	}
}